package org.jeecg.modules.yks.mng.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yks.mng.entity.YksAdviceMessage;
import org.jeecg.modules.yks.mng.mapper.YksAdviceMessageMapper;
import org.jeecg.modules.yks.mng.service.IYksAdviceMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 消息表
 * @Author: jeecg-boot
 * @Date:   2023-11-18
 * @Version: V1.0
 */
@Service
public class YksAdviceMessageServiceImpl extends ServiceImpl<YksAdviceMessageMapper, YksAdviceMessage> implements IYksAdviceMessageService {

    @Autowired
    private YksAdviceMessageMapper yksAdviceMessageMapper;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public int saveAndSend(YksAdviceMessage yksAdviceMessage) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        yksAdviceMessageMapper.insert(yksAdviceMessage);
        sendMsg(sysUser.getUsername(), yksAdviceMessage);
        return 0;
    }
    private void sendMsg(String username, YksAdviceMessage message) {
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setCategory("2");
        messageDTO.setContent(message.getContext());
        messageDTO.setFromUser(username);
        messageDTO.setTitle("用户消息");
        messageDTO.setToUser(message.getCreateBy());
        sysBaseAPI.sendSysAnnouncement(messageDTO);
    }
}
